package org.dromara.book.controller;

import java.util.List;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.book.domain.vo.SzlyBookInventoryVo;
import org.dromara.book.service.ISzlyBookInventoryService;
import org.dromara.common.core.utils.TreeBuildUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.book.domain.vo.SzlyBaseShelfVo;
import org.dromara.book.domain.bo.SzlyBaseShelfBo;
import org.dromara.book.service.ISzlyBaseShelfService;

/**
 * 层架管理
 * 前端访问路由地址为:/book/baseShelf
 *
 * @author LionLi
 * @date 2024-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/baseShelf")
public class SzlyBaseShelfController extends BaseController {

    private final ISzlyBaseShelfService szlyBaseShelfService;

    /**
     * 查询层架管理列表
     */
    @SaCheckPermission("book:baseShelf:list")
    @GetMapping("/list")
    public R<List<SzlyBaseShelfVo>> list(SzlyBaseShelfBo bo) {
        List<SzlyBaseShelfVo> list = szlyBaseShelfService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 层架下拉选择
     */
    @GetMapping("/list2Select")
    public R<List<Tree<Long>>> list2Select(SzlyBaseShelfBo bo) {
        List<SzlyBaseShelfVo> list = szlyBaseShelfService.queryList(bo);
        List<Tree<Long>> build = TreeUtil.build(list, 0L, (item, tree) ->
            tree.setId(item.getId())
                .setParentId(item.getParentId())
                .setName(item.getName()));
        return R.ok(build);
    }


    /**
     * 导出层架管理列表
     */
    @SaCheckPermission("book:baseShelf:export")
    @Log(title = "层架管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SzlyBaseShelfBo bo, HttpServletResponse response) {
        List<SzlyBaseShelfVo> list = szlyBaseShelfService.queryList(bo);
        ExcelUtil.exportExcel(list, "层架管理", SzlyBaseShelfVo.class, response);
    }

    /**
     * 获取层架管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("book:baseShelf:query")
    @GetMapping("/{id}")
    public R<SzlyBaseShelfVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(szlyBaseShelfService.queryById(id));
    }

    /**
     * 新增层架管理
     */
    @SaCheckPermission("book:baseShelf:add")
    @Log(title = "层架管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SzlyBaseShelfBo bo) {
        if(bo.getParentId()==null){
            bo.setParentId(0L);
        }
        return toAjax(szlyBaseShelfService.insertByBo(bo));
    }

    /**
     * 修改层架管理
     */
    @SaCheckPermission("book:baseShelf:edit")
    @Log(title = "层架管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SzlyBaseShelfBo bo) {
        if(bo.getParentId()==null){
            bo.setParentId(0L);
        }
        return toAjax(szlyBaseShelfService.updateByBo(bo));
    }

    private final ISzlyBookInventoryService iSzlyBookInventoryService;

    /**
     * 删除层架管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("book:baseShelf:remove")
    @Log(title = "层架管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        List<SzlyBookInventoryVo> inventoryVos = iSzlyBookInventoryService.queryByShelfIds(List.of(ids));
        if(!inventoryVos.isEmpty()){
            return R.warn("该层架还有图书，不能删除");
        }
        return toAjax(szlyBaseShelfService.deleteWithValidByIds(List.of(ids), true));
    }
}
